<?php

include_once 'Expression.php';

/**
 * Represents a reference to a field (property)
 * 
 * @package UC_PHPCodeModel
 * @author Uros Calakovic 
 * @example ../Classes/FieldReferenceExpressionExample.php
 */

class FieldReferenceExpression extends Expression
{
    /**
     * The FieldReferenceExpression constructor 
     * @param string $FieldName The name of the field to reference
     * @param Expression $TargetObject The object that contains the field
     */
    
    public function __construct($FieldName, Expression $TargetObject = null)
    {
        parent::__construct();
        
        $this->set_FieldName($FieldName);
        
        $TargetObject == null ? 
            $this->TargetObject = new Expression() :
            $this->set_TargetObject($TargetObject);
    }
    
    /**
     * Used for making private / protected variables 
     * accessible from outside the class.
     * 
     * @param string $name Name of the field to be accessed
     * @param mixed $value The value the field should be set to, usually a collection.
     * @see CodeObject::__set()
     */
    
    public function __set($name, $value)
    {
        parent::__set($name, $value);
        
        if($name == 'TargetObject')
        {
            $this->set_TargetObject($value);
        }
        
        if($name == 'FieldName')
        {
            $this->set_FieldName($value);
        }
    }
    
    /**
     * Used for making private / protected variables 
     * accessible from outside the class.
     * 
     * @param string $name Name of the field to be accessed
     * @return mixed The field that $name specified, usually a collection
     * @see CodeObject::__get()
     */
    
    public function __get($name)
    {
        $ret = parent::__get($name);
        
        if($ret != null)
        {
            return $ret;
        }
        
        if($name == 'TargetObject')
        {
            return $this->get_TargetObject();
        }
        
        if($name == 'FieldName')
        {
            return $this->get_FieldName();
        }
    }
    
    /**
     * The $FieldName getter method
     * @return string 
     */
    
    public function get_FieldName()
    {
        return $this->FieldName;
    }

    /**
     * The $FieldName setter method
     * @param string $FieldName
     * @throws InvalidArgumentException If the argument is not a string
     * 
     * @assert (123) throws InvalidArgumentException
     * @assert (true) throws InvalidArgumentException
     */
    
    public function set_FieldName($FieldName)
    {
        if(is_string($FieldName))
        {
            $this->FieldName = $FieldName;
        }
        else
        {
            throw new InvalidArgumentException(
                'The field name must be a string.');
        }
    }
    
    /**
     * The $TargetObject getter method
     * @return Expression 
     */

    public function get_TargetObject()
    {
        return $this->TargetObject;
    }
    
    /**
     * The $TargetObject setter method
     * @param Expression $TargetObject 
     * 
     * @assert (123) throws PHPUnit_Framework_Error
     * @assert ('name') throws PHPUnit_Framework_Error
     */

    public function set_TargetObject(Expression $TargetObject)
    {
        $this->TargetObject = $TargetObject;
    }

    /**
     * The name of the field to reference
     * @var string 
     */
    
    private $FieldName;
    
    /**
     * The object that contains the field
     * @var Expression 
     */
    
    private $TargetObject;
}

?>
